//
//  ZGWBiaoQianListViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/6/1.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ZGWBiaoQianListViewController.h"
#import "ZGWAddCustomBiaoQianViewController.h"

@interface ZGWBiaoQianListViewController ()

@property (nonatomic, strong) UIScrollView *backGroundScrollView; // 背景scrollView

@property (nonatomic, strong) NSMutableArray *biaoQianArray; // 标签数据源

@property (nonatomic, strong) NSMutableArray *systemBiaoQianArray; // 系统标签变动数组
@property (nonatomic, strong) NSMutableArray *systemBuBianBiaoQianArray; // 系统标签不变数组
@property (nonatomic, strong) NSMutableDictionary *selectSyetemWeiZhiDic; // 记录系统标签的位置

@end

@implementation ZGWBiaoQianListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 添加数据源
    [self addData];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"添加标签";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    // 右方
    UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeSystem];
    finishButton.frame = CGRectMake(0, 0, 50, 23);
    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
    [finishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [finishButton addTarget:self action:@selector(finishBiaoQian:) forControlEvents:UIControlEventTouchUpInside];
    finishButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k16Big];
    finishButton.layer.borderWidth = 1 * kAdjustParamater;
    finishButton.layer.borderColor = [[UIColor whiteColor] CGColor];
    finishButton.layer.cornerRadius = 6 * kAdjustParamater;
    finishButton.layer.masksToBounds = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:finishButton];
    
    
}

#pragma mark - 完成
- (void)finishBiaoQian:(UIButton *)button
{
    self.returnBiaoQianArray(self.biaoQianArray);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加数据源
- (void)addData
{
    // 建立模拟数据
    self.biaoQianArray = [NSMutableArray arrayWithObjects:@"健康达人", @"我是音乐达人", @"我是跑步达人", nil];
    
    self.systemBiaoQianArray = [NSMutableArray arrayWithObjects:@"系统标签一", @"系统标签二", @"系统标签三", @"系统标签四", @"系统标签五", @"系统标签六", @"系统标签七", nil];
    self.systemBuBianBiaoQianArray = [NSMutableArray arrayWithArray:self.systemBiaoQianArray];
    // 初始化系统标签位置存储字典
    self.selectSyetemWeiZhiDic = [NSMutableDictionary dictionary];
    for (int i = 0; i < self.systemBuBianBiaoQianArray.count; i++) {
        NSString *tempString = self.systemBuBianBiaoQianArray[i];
        [self.selectSyetemWeiZhiDic setValue:[NSString stringWithFormat:@"%d", i] forKey:tempString];
    }
}

#pragma mark - 布局
- (void)addMySubview
{
    self.backGroundScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    self.backGroundScrollView.backgroundColor = [UIColor whiteColor];
    self.backGroundScrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight);
    self.backGroundScrollView.bounces = NO;
    self.backGroundScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_backGroundScrollView];
    
    // 我的标签
    UILabel *mineBiaoQianLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 10 * kAdjustParamater, 100 * kAdjustParamater, 30 * kAdjustParamater)];
  //  mineBiaoQianLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    mineBiaoQianLabel.text = @"我的标签";
    mineBiaoQianLabel.textColor = kCyanColor;
    mineBiaoQianLabel.textAlignment = NSTextAlignmentLeft;
    mineBiaoQianLabel.font = [UIFont systemFontOfSize:k20Big];
    [self.backGroundScrollView addSubview:mineBiaoQianLabel];
    
    // 添加
    UIButton *addBiaoQianButton = [UIButton buttonWithType:UIButtonTypeSystem];
  //  addBiaoQianButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    addBiaoQianButton.frame = CGRectMake((375 - 45 - 20) * kAdjustParamater, 10 * kAdjustParamater, 45 * kAdjustParamater, 30 * kAdjustParamater);
    [addBiaoQianButton setTitle:@"添加" forState:UIControlStateNormal];
    [addBiaoQianButton setTitleColor:kCyanColor forState:UIControlStateNormal];
    addBiaoQianButton.titleLabel.font = [UIFont systemFontOfSize:k20Big];
    [addBiaoQianButton addTarget:self action:@selector(addCustomBiaoQian:) forControlEvents:UIControlEventTouchUpInside];
    [self.backGroundScrollView addSubview:addBiaoQianButton];
    
    // 我的标签下方的横条
    UILabel *cyanLineOnMineBiaoQianBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, mineBiaoQianLabel.bottom, kScreenWidth, 1 * kAdjustParamater)];
    cyanLineOnMineBiaoQianBottom.backgroundColor = kCyanColor;
    [self.backGroundScrollView addSubview:cyanLineOnMineBiaoQianBottom];
    
    // 我的标签的灰色背景
    UIView *grayViewOfMineBiaoQian = [[UIView alloc] initWithFrame:CGRectMake(0, cyanLineOnMineBiaoQianBottom.bottom, kScreenWidth, 150 * kAdjustParamater)];
    grayViewOfMineBiaoQian.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    grayViewOfMineBiaoQian.userInteractionEnabled = YES;
    [self.backGroundScrollView addSubview:grayViewOfMineBiaoQian];
    
    // 我的标签的动态布局
    [self addMineBiaoWithDongTaiOnView:grayViewOfMineBiaoQian];
    
    // 系统标签
    UILabel *systemBiaoQianLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, grayViewOfMineBiaoQian.bottom + 10 * kAdjustParamater, 100 * kAdjustParamater, 30 * kAdjustParamater)];
    systemBiaoQianLabel.text = @"系统标签";
    systemBiaoQianLabel.textColor = kCyanColor;
    systemBiaoQianLabel.textAlignment = NSTextAlignmentLeft;
    systemBiaoQianLabel.font = [UIFont systemFontOfSize:k20Big];
    [self.backGroundScrollView addSubview:systemBiaoQianLabel];
    
    // 系统标签下方的横条
    UILabel *cyanLineOnSystemBiaoQianBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, systemBiaoQianLabel.bottom, kScreenWidth, 1 * kAdjustParamater)];
    cyanLineOnSystemBiaoQianBottom.backgroundColor = kCyanColor;
    [self.backGroundScrollView addSubview:cyanLineOnSystemBiaoQianBottom];
    
    // 系统标签的灰色背景
    UIView *grayViewOfSystemBiaoQian = [[UIView alloc] initWithFrame:CGRectMake(0, cyanLineOnSystemBiaoQianBottom.bottom, kScreenWidth, 200 * kAdjustParamater)];
    grayViewOfSystemBiaoQian.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    [self.backGroundScrollView addSubview:grayViewOfSystemBiaoQian];
    [self addSystemBiaoQianWithDongTaiOnView:grayViewOfSystemBiaoQian];
    
    
 //   NSLog(@"系统横条的bottom = %f", cyanLineOnSystemBiaoQianBottom.bottom);
}

#pragma mark - 进行我的标签的动态布局
- (void)addMineBiaoWithDongTaiOnView:(UIView *)backgroundView
{
    // 存储下一个标签的Y开始位置
    CGFloat nextYStart = 0.0;
    for (int i = 0; i < self.biaoQianArray.count; i++) {
        NSInteger hang = (i) / 2;
        nextYStart = hang * (30 + 15) * kAdjustParamater;
        // 判断奇偶
        NSInteger jiOrOu = i % 2;
        // 标签背景
        UIView *biaoQianBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(15 * kAdjustParamater + jiOrOu * (15 + 165) * kAdjustParamater, 15 * kAdjustParamater + nextYStart, 165 * kAdjustParamater, 30 * kAdjustParamater)];
        biaoQianBackgroundView.userInteractionEnabled = YES;
        biaoQianBackgroundView.layer.cornerRadius = 4 * kAdjustParamater;
        biaoQianBackgroundView.layer.masksToBounds = YES;
        biaoQianBackgroundView.backgroundColor = kCyanColor;
        biaoQianBackgroundView.tag = 100 + i;
        [backgroundView addSubview:biaoQianBackgroundView];
        
        UILabel *biaoQianLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * kAdjustParamater, 0, 140 * kAdjustParamater, 30 * kAdjustParamater)];
        biaoQianLabel.text = self.biaoQianArray[i];
        biaoQianLabel.textColor = [UIColor whiteColor];
        biaoQianLabel.textAlignment = NSTextAlignmentLeft;
        biaoQianLabel.font = [UIFont systemFontOfSize:k16Big];
        [biaoQianBackgroundView addSubview:biaoQianLabel];
        
        // 错号
        UIButton *cuoHaoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        cuoHaoButton.frame = CGRectMake((165 - 20) * kAdjustParamater, 5 * kAdjustParamater, 20 * kAdjustParamater, 20 * kAdjustParamater);
        [cuoHaoButton setBackgroundImage:[UIImage imageNamed:@"newcuohao-liaotianlieibao"] forState:UIControlStateNormal];
        cuoHaoButton.tag = 200 + i;
        [cuoHaoButton addTarget:self action:@selector(cancelSelect:) forControlEvents:UIControlEventTouchUpInside];
        cuoHaoButton.layer.cornerRadius = cuoHaoButton.width / 2;
        cuoHaoButton.layer.masksToBounds = YES;
        [biaoQianBackgroundView addSubview:cuoHaoButton];
    
        
    }
    
    // 取出最后一个标签的背景
    UIView *lastMineBiaoQianBackgroundView = (UIView *)[self.view viewWithTag:99 + self.biaoQianArray.count];
    backgroundView.height = lastMineBiaoQianBackgroundView.bottom + 15 * kAdjustParamater;
  //  NSLog(@"我的标签背景bottom = %f", backgroundView.bottom);
}

#pragma mark - 进行系统标签的动态布局
- (void)addSystemBiaoQianWithDongTaiOnView:(UIView *)backGroundView
{
    // 存储下一个标签的Y开始位置
    CGFloat nextYStart = 0.0;
    for (int i = 0; i < self.systemBiaoQianArray.count; i++) {
        NSInteger hang = (i) / 2;
        nextYStart = hang * (30 + 15) * kAdjustParamater;
        // 判断奇偶
        NSInteger jiOrOu = i % 2;
        // 标签背景
        UIView *biaoQianBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(15 * kAdjustParamater + jiOrOu * (15 + 165) * kAdjustParamater, 15 * kAdjustParamater + nextYStart, 165 * kAdjustParamater, 30 * kAdjustParamater)];
        biaoQianBackgroundView.userInteractionEnabled = YES;
        biaoQianBackgroundView.layer.cornerRadius = 4 * kAdjustParamater;
        biaoQianBackgroundView.layer.masksToBounds = YES;
        biaoQianBackgroundView.backgroundColor = kCyanColor;
        biaoQianBackgroundView.tag = 300 + i;
        [backGroundView addSubview:biaoQianBackgroundView];
        
        UITapGestureRecognizer *biaoQianBackGroundViewTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(biaoQianBackGroundViewGes:)];
        [biaoQianBackgroundView addGestureRecognizer:biaoQianBackGroundViewTapGes];
        
        
        UILabel *biaoQianLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * kAdjustParamater, 0, 140 * kAdjustParamater, 30 * kAdjustParamater)];
        biaoQianLabel.text = self.systemBiaoQianArray[i];
        biaoQianLabel.textColor = [UIColor whiteColor];
        biaoQianLabel.textAlignment = NSTextAlignmentLeft;
        biaoQianLabel.font = [UIFont systemFontOfSize:k16Big];
        [biaoQianBackgroundView addSubview:biaoQianLabel];
        
        
        
        
    }
    // 取出最后一个view
    UIView *lastBackGroundView = (UIView *)[self.view viewWithTag:299 + self.systemBiaoQianArray.count];
//    NSLog(@"屏幕高度 = %f", kScreenHeight);
    if (lastBackGroundView.bottom < kScreenHeight) {
        backGroundView.height = 600 * kAdjustParamater;
        self.backGroundScrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight);
    } else {
        backGroundView.bottom = lastBackGroundView.bottom + 15 * kAdjustParamater;
        self.backGroundScrollView.contentSize = CGSizeMake(kScreenWidth, backGroundView.bottom + 20 * kAdjustParamater);
    }

}

#pragma mark - 添加自定义标签
- (void)addCustomBiaoQian:(UIButton *)button
{
  //  NSLog(@"添加自定义标签");
    ZGWAddCustomBiaoQianViewController *zgwAddCustomBiaoQianVC = [[ZGWAddCustomBiaoQianViewController alloc] init];
    __block ZGWBiaoQianListViewController *mySelf = self;
    zgwAddCustomBiaoQianVC.addCustomBiaoQian = ^void (NSString *customBiaoQian) {
        
        // 为标签数组添加自定义标签
        if (mySelf.biaoQianArray.count < 6) {
            [mySelf.biaoQianArray addObject:customBiaoQian];
            // 重新布局
            [self addMySubview];
        } else {
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提醒" Content:@"标签不能超过6个" ToView:self];
        }
    };
    [self.navigationController pushViewController:zgwAddCustomBiaoQianVC animated:YES];
}

#pragma mark - 取消标签
- (void)cancelSelect:(UIButton *)button
{
//    NSLog(@"取消的第%ld个标签", button.tag - 200);
    NSInteger selectMine = button.tag - 200;
    
    // 在系统标签数组里面添加点击的标签
    // 判断此标签是否在系统标签中存在
    for (int i = 0; i < self.systemBuBianBiaoQianArray.count; i++) {
        NSString *tempString = self.systemBuBianBiaoQianArray[i];
        if ([tempString isEqualToString:self.biaoQianArray[selectMine]]) {
            // 说明选择的标签是系统的
            // 获取此标签在原系统标签的位置
            NSInteger weiZhi = [[self.selectSyetemWeiZhiDic valueForKey:tempString] integerValue];
            if (self.systemBiaoQianArray.count > selectMine) {
                            [self.systemBiaoQianArray insertObject:self.biaoQianArray[selectMine] atIndex:weiZhi];
            } else {
                [self.systemBiaoQianArray addObject:self.biaoQianArray[selectMine]];
            }

        }
    }
    // 在我的标签数组里面删除点击的标签
    [self.biaoQianArray removeObjectAtIndex:selectMine];
    // 刷新界面
    [self addMySubview];
}

#pragma mark - 点击系统标签
- (void)biaoQianBackGroundViewGes:(UITapGestureRecognizer *)tapGes
{
 //   NSLog(@"点击系统的第%ld个标签", tapGes.view.tag - 300);
    NSInteger selectSystem = tapGes.view.tag - 300;
    if (self.biaoQianArray.count < 6) {
        // 在我的标签数组里面增加点击的标签
        [self.biaoQianArray addObject:self.systemBiaoQianArray[selectSystem]];
        // 在系统标签数组里面删除点击的标签
        [self.systemBiaoQianArray removeObjectAtIndex:selectSystem];
        // 刷新界面
        [self addMySubview];
    } else {
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提醒" Content:@"标签不能超过6个" ToView:self];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
